home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH13 / C13_4.ASM < prev    next >
Assembly Source File  |  1994-11-15  |  5KB  |  127 lines

  1. ;
  2. ; Program 13.4 Loading a Child
  3. ;
  4. .286
  5. dosseg
  6. .model small
  7. .stack 100h
  8. .data
  9. comm_tail  db 3,' :',0Dh             ; Command tail
  10. prog_name  db '\windows\win.com',0
  11.  
  12. ; EXEC parameter block
  13.            align 2
  14. Env        dw 0                         ; parent's environment
  15. comlin     dw offset comm_tail,0
  16. FCB1       dw 5ch,0                     ; 5Ch is FCB1 offset in PSP
  17. FCB2       dw 6ch,0                     ; 6Ch is FCB2 offset in PSP
  18. SS_SP      dw 0,0                       ; Child's stack pointer
  19. CS_IP      dw 0,0                       ; Child's entry point
  20.  
  21. ok_mess    db 'Alright!',10,13,'$'
  22.  
  23. ret_val    db 0
  24. method     db 0
  25.  
  26. .code
  27. ; Use code segment to store variables for invoking child program
  28. ;
  29. save_ds    dw 0                         ; Parent's DS
  30. save_sp    dw 0                         ; Parent's SP
  31. save_ss    dw 0                         ; Parent's SS
  32. child_ptr  dw 0,0                       ; Child's starting address
  33. Start:
  34.                 mov  ax,@data
  35.                 mov  ds,ax              ; Load data segment
  36.                 call Init               ; Resize program memory block
  37.                 jnc  Init_OK
  38.                 jmp  Exit               ; Exit on error
  39. Init_OK:
  40.                 mov comlin+2,ds         ; Load segment of command tail
  41.                 mov FCB1+2,es           ; Copy FCBs from parent's PSP
  42.                 mov FCB2+2,es
  43.                 mov ax,es:[2ch]         ; Load environment segment
  44.                 mov Env,ax              ; from parent's PSP
  45.  
  46.                 mov ax,4b01h
  47.                 mov dx,offset prog_name ; DS:DX points to child pathname
  48.                 mov cx,ds
  49.                 mov es,cx
  50.                 mov bx,offset Env       ; ES:BX points to EXEC parameter
  51.                                         ; block
  52.                 int 21h                 ; Load child
  53.                 jc Exit                 ; CF set if error
  54.  
  55.                 mov cs:save_ds,ds       ; Save DS in code segment
  56.                 mov cs:save_ss,ss       ; Save parent's stack pointer also
  57.                 mov cs:save_sp,sp
  58.  
  59.                 mov ax,CS_IP            ; Copy child pointer to code segment
  60.                 mov child_ptr,ax        ; so can reference it after changing
  61.                 mov ax,CS_IP+2          ; DS, ES, SS
  62.                 mov child_ptr+2,ax
  63.  
  64.                 mov ah,62h
  65.                 int 21h                 ; Get current PSP (child's) in BX
  66.  
  67.                 cli
  68.                 mov ss,SS_SP+2          ; Load child's stack
  69.                 mov sp,SS_SP
  70.                 sti
  71.  
  72.                 mov es,bx               ; ES and DS must point to the
  73.                 mov ds,bx               ; child PSP before invoking it
  74.  
  75.                 ; put return address in child's PSP
  76.                 mov word ptr es:[10],offset cs:addr_back
  77.                 mov es:[12],cs
  78.  
  79.                 jmp dword ptr cs:child_ptr   ; Run child program
  80.  
  81. addr_back:
  82.                 cli
  83.                 mov ss,cs:save_ss       ; Restore parent's stack
  84.                 mov sp,cs:save_sp
  85.                 sti
  86.                 mov ds,cs:save_ds       ; Restore parent's DS
  87.  
  88.                 mov ah,4dh
  89.                 int 21h                 ; Get child-program return value
  90.                 mov ret_val,al          ; AL = return value
  91.                 mov method,ah           ; AH = termination method
  92.  
  93.                 mov ah,9
  94.                 mov dx,offset ok_mess
  95.                 int 21h
  96.                 xor al,al               ; Successful
  97. Exit:
  98.                 mov ah,4ch
  99.                 int 21h
  100.  
  101. Init            proc
  102. ; Input:  ax=ds, es=PSP segment, SS:SP as set by DOS
  103. ; Output: SS:SP adjusted, CF=0 if resize is successful
  104. ;
  105.                 mov bx,ss
  106.                 sub bx,ax
  107.                 shl bx,4
  108.                 cli
  109.                 mov ss,ax               ; Load stack pointer
  110.                 add sp,bx
  111.                 sti
  112.  
  113.                 mov bx,sp
  114.                 add bx,15               ; Round up to next paragraph
  115.                 shr bx,4
  116.                 add ax,bx               ; AX = SS + SP / 16 = segment address
  117.                                         ; of the end of the program space
  118.                 mov bx,es
  119.                 sub ax,bx               ; AX = required ammount of paragraphs
  120.                 mov bx,ax
  121.                 mov ah,4ah
  122.                 int 21h                 ; Resize program
  123.                 ret
  124. Init            endp
  125.  
  126. end Start
  127.